﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Threading;
using System.Net.Sockets;

namespace SmartPhoneClient
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        public Form3(string s)
        {
            InitializeComponent();
            tableID = s;
        }

        string tableID = "";    // 默认桌号为0
        ArrayList order = new ArrayList();

        private void Form3_Load(object sender, EventArgs e)
        {
            this.Text = "开桌 - 桌号：" + tableID.ToString();
        }

        private void Form3_GotFocus(object sender, EventArgs e)
        {
            this.treeView1.Nodes.Clear();
            for (int i = 0; i < Infomation.Order.Count; i++)
            {
                string[] orderInfo = ((string)Infomation.Order[i]).Split(new Char[] { '|' });
                treeView1.Nodes.Add("桌号：" + orderInfo[1] + "  菜号：" + orderInfo[2] + "  厨师号：" + orderInfo[3]);
            }
        }

        /// <summary>
        /// “添菜”按钮单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItem3_Click(object sender, EventArgs e)
        {
            string foodID = textBox1.Text;
            string cookID = textBox2.Text;
            string custNotes = textBox3.Text;

            if (Infomation.MySocket.Connected == true)
            {
                string command = "CheckFood|" + tableID + "|" + foodID + "|" + cookID + "|" + custNotes;
                Byte[] outbytes = System.Text.Encoding.Unicode.GetBytes(command.ToCharArray());
                Infomation.MySocket.Send(outbytes, outbytes.Length, 0);
                // 接下来由Form1的Receive线程来处理
            }
        }


        /// <summary>
        /// 单击“删菜”按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItem4_Click(object sender, EventArgs e)
        {
            if ((textBox1.Text == "") || (textBox1.Text == "请输入菜号"))
            {
                textBox1.Text = "请输入菜号";
            }
            else
            {
                int deletedID = -1;
                int count = Infomation.Order.Count; //防止执行了Infomation.Order.RemoveAt(i)语句之后造成的循环次数判断错误

                for (int i = 0; i < count; i++)
                {
                    string[] s = ((string)Infomation.Order[i]).Split(new Char[] { '|' });
                    string foodID = s[2];
                    if (foodID == textBox1.Text)
                    {
                        deletedID = i;
                        break;
                    }
                }
                if (deletedID != -1)
                {
                    Infomation.Order.RemoveAt(deletedID);
                    this.Focus();
                }
            }
        }

        /// <summary>
        /// 单击“发送”按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItem2_Click(object sender, EventArgs e)
        {
            int count = Infomation.Order.Count;
            for (int i = 0; i < count; i++)
            {
                string command = (string)Infomation.Order[i];

                string[] s = command.Split(new Char[] { '|' });
                string foodID = s[2];
                MessageBox.Show("第 " + foodID + "号菜发送成功");

                Byte[] outbytes = System.Text.Encoding.Unicode.GetBytes(command.ToCharArray());
                Infomation.MySocket.Send(outbytes, outbytes.Length, 0);
            }
            menuItem2.Enabled = false;
            MessageBox.Show("菜单已经全部发送");
            this.Close();
        }


    }
}